//
// Created by 夏申频 on 17/1/26.
//

#include "ImageCodec.h"

#include <fstream>
#include <sstream>
const int bufferSize = 1024;
const char* fileName = "/Users/zarra/IdeaProjects/jjpeg/src/test/resources/lena512Gray.raw";

int main(){

    std::fstream in;
    //in.open("/Users/zarra/ClionProjects/image_codec/jplossless.raw",std::ios::binary|std::ios::in);
    //in.open("./jplossless.raw", std::ios::binary | std::ios::in);
    in.open(fileName, std::ios::binary | std::ios::in);

    std::ostringstream oBuffer;

    char buffer[bufferSize];

    int readN = 0;
    do{
        in.read(buffer,bufferSize);
        readN = in.gcount();
        if (readN>0){
            oBuffer.write(buffer,readN);
        }
    }while (readN>0);

    ImageCodecErr err;
    ImageMeta meta;

    ImageCodecContext c;
    char* output;
    long outputSize;

    char* compressOutput;
    long compressOutputSize;

    std::string oString = oBuffer.str();
    c.input = oString.data();
    c.inputSize = oString.size();
    c.meta = &meta;
    c.output = &output;
    c.outputSize = &outputSize;


    decode(CJPEG8,&c,&err);
    printf("err:%d\n",err.code);

    c.extra[LOSSLESS] = false;
    c.extra[QUALITY] = 80;

    c.input = output;
    c.inputSize = outputSize;
    c.output = &compressOutput;
    c.outputSize = &compressOutputSize;

    encode(CJPEG8,&c,&err);
    printf("err:%d\n",err.code);
    std::fstream fout;

    fout.open("./test.jpg",std::ios::binary | std::ios::out);

    fout.write(compressOutput,compressOutputSize);

    fout.close();



    free(output);
    free(compressOutput);
    printf("err:%d",err.code);
}